# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/9/21
desc: 
sample: 
"""
import random

info1 = {
    "age": 18,
    "status": True,
    "name": "极光"
}

info2 = {}
info3 = dict()

# print(info1.get('age1','0'))
# print(info2.get('age','0'))
# print(info1['age']) #key 报错
# print("---------")
# print(info2['age'])
# info1['pwd']=12
# print(info1)
#
print(info1.keys())
# 元祖
print('---------', type(info1.keys()))
for test in info1.keys():
    print(test)
# print(info1.values())
# print(info1.items())
#
print(info1.items())
# for item in info1.items():
#     print(item[0],item[1])
# print("-------")
# for k,v in info1.items():
#     print(k,v)

# 统计下优秀成绩同学人数
score = ['不及格', '良好', '优秀']
db_list = [('zhangsan', random.randint(16, 25), random.choice(score)),
           ('李四', random.randint(16, 25), random.choice(score)),
           ('王五', random.randint(16, 25), random.choice(score)),
           ('赵六', random.randint(16, 25), random.choice(score))]
print(db_list)
counter = 0
for item in db_list:
    if item[2] == '优秀':
        counter += 1

print(f'优秀的学生个数{counter}')

counter = 0
for username, age, score in db_list:
    if score == '优秀':
        counter += 1
print(f'优秀的学生个数{counter}')

# 过滤出成绩为“优秀”的学生
excellent_students = [student for student in db_list if student[2] == '优秀']
print(f'优秀的学生个数{len(excellent_students)}')
print(excellent_students)

print("filter 方法")


# 函数方法 filter
# 定义一个函数来判断成绩是否为“优秀”
def is_excellent(student):
    _, _, score = student
    return score == '优秀'


# 使用 filter() 函数过滤出成绩为“优秀”的学生
excellent_students = list(filter(is_excellent, db_list))
print(excellent_students)

print(' lambda ---')
excellent_students = list(filter(lambda x: x[2] == '优秀', db_list))
print(excellent_students)


# 函数编程
def func1(a1, a2):
    return a1 + a2


func2 = lambda a1, a2: a1 + a2

# functest=func(1,2)
print(func1(1, 2))
print(func2(1, 2))

# 测试用例设计 过滤 排序
test = [{'caseName': '测试1', 'order': 2, '开启': '是'}, {'caseName': '测试2',
                                                     'order': 1, '开启': '是'},
        {'caseName': '测试3', 'order': 4, '开启': '否'}, {'caseName': '测试4',
                                                     'order': 3, '开启': '是'}]

# 过滤没有开启
# 传统
test2 = []
for item in test:
    if item.get('开启') == '是':
        # print('item------',type(item),item)
        test2.append(item)
print('过滤后的集合', test2)

# 结合函数
def is_open(item):
    return item.get('开启') == '是'

test3 = list(filter(is_open,test))
print('函数支持',test3)

# 结合 lambda 函数 更简化
test4 = list(filter(lambda item:item.get('开启') == '是',test))
print('lambda函数支持',test3)
#推导式 过滤
filtered_test = [item for item in test if item.get('开启') == '是']

#排序
def is_open(item):
    return item.get('order')

test5 = list(sorted(test4,key=is_open,reverse=True))
print('排序后',test5)

#结合 lambda
test6 = list(sorted(test4,key=lambda x:x.get('order')))
print('排序后',test6)